/**
 *	Encuestas Navarra Módulo principal.
 *	@version 0.0.1
 *	@author Gary Rios
 */

window.main = (function() {

// vars
var scrollable, timer,
	done = 0,
	total = 0,
	currentDone = 0,
	currentTotal = 0;	
	
/**
 *	Init
 */
function init() {
	// window
	$(window).on({
	resize: function() {
		if(scrollable) scrollable.seekTo(scrollable.getIndex(), 0);
	}});
	// scrollable
	scrollable = $("form").scrollable({keyboard: false}).data("scrollable");
	// skin
	$("input:radio").each(function(index, element) {
		var $this = $(this);
		$this.wrap('<span class="radio" />').css({"opacity": 0, "position": "absolute"}).parent().append($this.val());
	});
	// btn
	$("a.btn-prev").on("click", prev);
	$("a.btn-next").on("click", next);
	$("input:button").on("click", next);
	// keyboard
	$(document).on("keydown", function(e) {
		switch(e.which) {
		case 13:
		case 37:
		case 39:
			e.preventDefault();	
		}
	});
	$(document).on("keyup", function(e) {
		var target = currentItem().find(".question");
		if(e.which === 37) prev();
		if(e.which === 13 || e.which === 39) next();
		if(currentItem().find(".question").length > 1) target = target.not(".done").eq(0);
		target.find("label").filter(function() {
			return ($(this).find("input").val() === String.fromCharCode(e.which) || $(this).find("input").val() === String.fromCharCode(e.which - 48));
		}).find("input").trigger("click").trigger("change");
	});
	// input
	$("input:radio").on("change", function() {
		var $this = $(this);
		$this.closest(".question").find("label").removeClass("checked");
		$this.closest("label").addClass("checked").closest(".question").addClass("done");
		update();
		if(currentItem().prevAll().andSelf().find(".question").length === currentItem().prevAll().andSelf().find(".done").length) startCountdown();
	});
	$("#main-form").on("click", "label", function(e) {
		if($(this).hasClass("disabled")) e.preventDefault();
	});
	// update
	update();
	
	function update() {
		var percent;
		total = $(".question").length;
		done = $(".done").length,
		percent = done / total * 100;
		$("#progress-text").text(done + " de " + total + " preguntas respondidas");
		$("#progress-bar").children().css("width", percent + "%");
		$(".question").each(function(i) {
			var label = $(this).find("label");
			if(i <= done) label.removeClass("disabled");
			else label.addClass("disabled");
		});
	}
	
	function currentItem() {
		return scrollable.getItems().eq(scrollable.getIndex());
	}
	
	function startCountdown() {
		clearTimeout(timer);
		$("#timer").css("visibility", "visible");
		timer = setTimeout(next, 700);
	}
	
	function prev() {
		clearTimeout(timer);
		scrollable.prev();
		$("#timer").removeAttr("style");
	}
	
	function next() {
		currentTotal = currentItem().prevAll().andSelf().find(".question").length,
		currentDone = currentItem().prevAll().andSelf().find(".done").length;
		if(currentDone === currentTotal) {
			clearTimeout(timer);
			scrollable.next();
			$("#timer").removeAttr("style");
		}
	}
}

return {
	init: init	
}})();

window.main.init();